package com.example.dianproject.service.impl;

import com.example.dianproject.entity.ElectricityPrice;
import com.example.dianproject.repository.ElectricityPriceRepository;
import com.example.dianproject.service.ElectricityPriceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
public class ElectricityPriceServiceImpl implements ElectricityPriceService {
    
    @Autowired
    private ElectricityPriceRepository electricityPriceRepository;
    
    @Override
    @Transactional
    public ElectricityPrice savePrice(ElectricityPrice price) {
        return electricityPriceRepository.save(price);
    }
    
    @Override
    public BigDecimal getCurrentResidentialPrice() {
        return electricityPriceRepository.findCurrentPriceByType("RESIDENTIAL")
                .map(ElectricityPrice::getUnitPrice)
                .orElse(BigDecimal.ZERO);
    }
    
    @Override
    public BigDecimal getCurrentCommercialPrice() {
        return electricityPriceRepository.findCurrentPriceByType("COMMERCIAL")
                .map(ElectricityPrice::getUnitPrice)
                .orElse(BigDecimal.ZERO);
    }
    
    @Override
    public List<ElectricityPrice> getHistory() {
        return electricityPriceRepository.findAllOrderByEffectiveDateDesc();
    }
} 